﻿using System;
using System.Xml;
using System.Collections.Generic;

namespace AnaViewer
{
    /// <summary>
    /// 表示 RSS 2.0 XML 文档中的 Item 元素。
    /// 一个 RssChannel 中包含零个或多个 RssItem。
    /// </summary>
    public class RssItem : IItem
    {
        private readonly string title;
        private readonly string description;
        private readonly string link;
        private readonly string content;
        private readonly DateTime created;
        private readonly string author;
        private readonly List<string> categories;
        /// <summary>
        /// 评论链接。
        /// </summary>
        private readonly string comments;
        private readonly string commentRss;

        public string Title { get { return title; } }
        public string Description { get { return description; } }
        public string Link { get { return link; } }
        public DateTime Created { get { return created; } }


        public string Comments { get { return comments; } }

        /// <summary>
        /// 文本格式的 创建时间。
        /// </summary>
        public string PubDate
        {
            get
            {
                string time;
                if (created == DateTime.MinValue)
                    time = "时间不详";
                else
                    time = "公元" + created.ToLongDateString();
                return time;
            }
        }

        public string Author { get { return author; } }
        public System.Drawing.Image FaceImage { get { return null; } }

        public string Content { get{return content;}}
        public IList<string> Categories { get { return categories.AsReadOnly(); } }

        /// <summary>
        /// 根据表示 RSS 2.0 XML 文档内的一个 Item 元素的 XmlNode 生成一个 RSSItem。
        /// </summary>
        /// <param name="itemNode"></param>
        internal RssItem(XmlNode itemNode)
        {
            XmlNamespaceManager xnm = new XmlNamespaceManager(itemNode.OwnerDocument.NameTable);
            xnm.AddNamespace("content","http://purl.org/rss/1.0/modules/content/");
            xnm.AddNamespace("wfw","http://wellformedweb.org/CommentAPI/");
            xnm.AddNamespace("dc","http://purl.org/dc/elements/1.1/");
            xnm.AddNamespace("atom","http://www.w3.org/2005/Atom");

            XmlNode selected;
            selected = itemNode.SelectSingleNode("title",xnm);
            if (selected != null)
                title = selected.InnerText;

            selected = itemNode.SelectSingleNode("description", xnm);
            if (selected != null)
                description = selected.InnerText;

            selected = itemNode.SelectSingleNode("link", xnm);
            if (selected != null)
                link = selected.InnerText;

            selected = itemNode.SelectSingleNode("comments", xnm);
            if (selected != null)
                comments = selected.InnerText;

            selected = itemNode.SelectSingleNode("pubDate", xnm);
            if (selected != null)
                DateTime.TryParse(selected.InnerText, out created);
            else
                created = DateTime.MinValue;

            selected = itemNode.SelectSingleNode("dc:creator", xnm);
            if (selected != null)
                author = selected.InnerText;
            
            categories = new List<string>();
            foreach (XmlNode node in itemNode.SelectNodes("category", xnm))
                categories.Add(node.InnerText);

            selected = itemNode.SelectSingleNode("content:encoded", xnm);
            if (selected != null)
                content = selected.InnerText;

            selected = itemNode.SelectSingleNode("wfw:commentRss", xnm);
            if (selected != null)
                commentRss = selected.InnerText;
        }
    }
}